// 扁平数组转树状结构
module.exports.setTreeData = function (source) {
  let cloneData = JSON.parse(JSON.stringify(source))      // 对源数据深度克隆
  return cloneData.filter(father => {                      // 循环所有项，并添加children属性
    let branchArr = cloneData.filter(child => father._id == child.pid);   // 返回每一项的子级数组
    branchArr.length > 0 ? father.children = branchArr : ''   //给父级添加一个children属性，并赋值
    return father.pid == 0;      //返回第一层
  });
}

//断言
module.exports.assert = (res, exp, msg) => {
  if (!exp) {
    console.log(msg || 'ASSERT ERROR');
    res.send({
      msg: msg || 'ASSERT ERROR',
      status: 202,
    });
    throw new Error(msg || 'ASSERT ERROR')
  }
}